//
//  SelectVocaViewController.m
//  i18n
//
//  Created by Earus on 11/28/11.
//  Copyright (c) 2011 patrick.decaluwe@student.uclouvain.be. All rights reserved.
//

#import "SelectVocaViewController.h"

@implementation SelectVocaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        fileList = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    // enumerate하면서, 책갈피 제외하고 다 삭제한다.
	NSString *file;
	NSString *docsDir = [[NSString alloc] initWithFormat: @"%@/Documents", NSHomeDirectory()];
	NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:docsDir];
	
	while ((file = [dirEnum nextObject])) {
        
        if ([file hasSuffix:@"xml"]) {
            NSLog(@"%@", file);
            //NSString *path = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), file];           
            
            [fileList addObject:file];
        }
        
	}
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)dismiss {
    [self dismissModalViewControllerAnimated:YES];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [fileList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSString *filename = [fileList objectAtIndex:indexPath.row];
    cell.textLabel.text = filename;
    
    NSString *org_selected = [[NSUserDefaults standardUserDefaults] objectForKey:@"selected_wordset"];
    
    if ([org_selected isEqualToString:filename]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *filename = [fileList objectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults] setObject:filename forKey:@"selected_wordset"];    
    
    [tableView reloadData];
}

@end
